<?
class Json {
	public static function toJson($obj) {
		switch (gettype($obj)) {
			case "boolean":
			case "integer":
			case "double":
			case "NULL":
				return $obj;
				break;
			case "string":
				return "'" . addslashes($obj) . "'";
				break;
			case "array":
				$str = "[";
				foreach($obj as $item) {
					$str .= $comma . Json::toJson($item);
					$comma = ",";
				}
				$str .= "]";
				return $str;
				break;
			case "object":
				return Json::objToJson($obj);
				break;
		}
	}
	
	private static function objToJson($obj) {
		$ref = new ReflectionObject($obj);
		
		$str = "{";
		foreach ($ref->getMethods() as $method) {
			$name = $method->getName();
			if (substr($name, 0, 3) == "get") {
				$property = strtolower(substr($name, 3));
				$str .= $comma . $property . ":" . Json::toJson($method->invoke($obj));
				$comma = ",";
			}
		}
		$str .= "}";
		
		return $str;
	}
}
?>
